package com.xinminda.dcf.ui.base;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import com.xinminda.dcf.utils.TUtil;
import com.xinminda.dcf.utils.ToastUtil;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;


public abstract class BaseActivity <T extends BasePresenter, E extends BaseModel>extends AppCompatActivity {

    private static final List<Activity> activityList = new ArrayList();
    public T mPresenter;
    public E mModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        addActivity(this);
        ButterKnife.bind(this);
        mPresenter = TUtil.getT(this, 0);
        mModel= TUtil.getT(this,1);
        if(mPresenter!=null){
            mPresenter.mContext=this;
        }
        this.initPresenter();
        this.init();
    }

    protected abstract int getLayout();

    protected abstract void init();

    public abstract void initPresenter();

    protected void startActivity(Class<?> clazz){
        startActivity(clazz, null);
    }

    protected void startActivity(Class<?> clazz, Bundle bundle){
        Intent intent = new Intent();
        intent.setClass(this, clazz);
        if(bundle != null){
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    protected void startActivity(String packageName, String className){
        Intent intent = new Intent();
        ComponentName cn = new ComponentName(packageName, className);
        intent.setComponent(cn);
        startActivity(intent);
    }

    protected void  startActivityForResult(Class<?> clazz, int resultCode) {
        startActivityForResult(clazz, resultCode, null);
    }

    protected void startActivityForResult(Class<?> clazz, int resultCode, Bundle bundle){
        Intent intent = new Intent();
        intent.setClass(this, clazz);
        if(bundle != null){
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, resultCode);
    }


    /**
     * 短暂显示Toast提示(来自String)
     **/
    public void showShortToast(String text) {
        ToastUtil.showShort(text);
    }

    /**
     * 短暂显示Toast提示(id)
     **/
    public void showShortToast(int resId) {
        ToastUtil.showShort(resId);
    }

    /**
     * 长时间显示Toast提示(来自res)
     **/
    public void showLongToast(int resId) {
        ToastUtil.showLong(resId);
    }

    /**
     * 长时间显示Toast提示(来自String)
     **/
    public void showLongToast(String text) {
        ToastUtil.showLong(text);
    }



    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.onDestroy();
        removeActivity(this);
    }

    protected void removeAll() {
        for (Activity activity : activityList) {
            if (!activity.isFinishing()) {
                activity.finish();
        }
        }
        activityList.clear();
    }

    protected void removeActivity(Activity activity) {
        activityList.remove(activity);
    }

    protected void addActivity(Activity activity){
        activityList.add(activity);
    }
}
